﻿using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using SharppiFlowEditor.models;
using SharppiFlowEditor.views;

namespace SharppiFlowEditor.controllers
{
    public class CreateNodeController : AbstractController
    {
        public delegate void GraphSelectEventHandler(object sender, FlowChartEventArgs e);

        private int idx = 0;
        private int _mode = 1;
        private double zoomValue;

        private AbstractNode moving = null;
        private int offsx, offsy;

        public CreateNodeController(AppView appView, AppModel appModel)
        {
            Model = appModel;
            View = appView;
        }

        /*
         * Create a new node in here.
         */
        override public void ViewMouseDown(object sender, MouseEventArgs e)
        {
            zoomValue = View.document.ZoomSize;
            scrollOffset = View.document.ViewScrollPosition;
            int x = e.X - scrollOffset.X;
            x = (int)(x / zoomValue);
            int y = e.Y - scrollOffset.Y;
            y = (int)(y / zoomValue);

            AbstractNode an = null;


            if (e.Button == MouseButtons.Left)
            {
                if (idx == 0)
                    an = Model.createAbstractNodeRENAMETHISMETHODLATER(View.document.GraphDocument, x, y, 20, 20, "default", 1);
                else
                    an = Model.createAbstractNodeRENAMETHISMETHODLATER(View.document.GraphDocument, x, y, 20, 20, "default", 2); //muokattava käyttämään modea!
                idx++;

                an.setBoundingRectLocation(x - (an.getBoundingRect().Width / 2), y - (an.getBoundingRect().Height/ 2)); 
            
                moving = an;
                offsx = (int)((e.X - scrollOffset.X) / zoomValue) - moving.getBoundingRect().X;
                offsy = (int)((e.Y - scrollOffset.Y) / zoomValue) - moving.getBoundingRect().Y;
                View.document.SelectedObject = an;
                View.document.Invalidate();
                View.document.Update();
            }
            else if (View.document.GetNode(x, y) != null) // right click
            {
                an = View.document.GetNode(x, y);
                int previousType = an.Type;
                if (previousType == 3)
                {
                    previousType = 0;
                }
                int x1 = an.getBoundingRect().X;
                int y1 = an.getBoundingRect().Y;
                Model.DeleteNode(View.document.GraphDocument, an);

                an = Model.createAbstractNodeRENAMETHISMETHODLATER(View.document.GraphDocument, x1, y1, 20, 20, "default", previousType + 1);
            }
            
            View.document.Invalidate();
            View.document.Update();

        }

        public override void ViewMouseMove(object sender, MouseEventArgs e)
        {
            scrollOffset = View.document.ViewScrollPosition;
            zoomValue = View.document.ZoomSize;

            if (moving != null)
            {
                if (View.document.GraphDocument.SnapToGridState.Equals(true))
                {
                    int x = (int)((e.X - scrollOffset.X) / zoomValue) - offsx;
                    int y = (int)((e.Y - scrollOffset.Y) / zoomValue) - offsy;

                    Model.MoveNode(View.document.GraphDocument, moving, View.document.GraphDocument.SnapX(x), View.document.GraphDocument.SnapY(y));
                }
                else
                {
                    Model.MoveNode(View.document.GraphDocument, moving, (int)(((e.X - scrollOffset.X)) / zoomValue) - offsx, (int)(((e.Y - scrollOffset.Y)) / zoomValue) - offsy);
                }
            }
        }

        public override void ViewMouseUp(object sender, MouseEventArgs e)
        {
            if (moving != null)
            {
                moving = null;
            }
        }

        public override void setMode(int newMode)
        {
            _mode = newMode;
        }

    }
}
